package com.example.contestsystem.Service;

import com.example.contestsystem.Dao.ContestRepository;
import com.example.contestsystem.Dao.JoinTeamRepository;
import com.example.contestsystem.Dao.TeamRepository;
import com.example.contestsystem.Entity.Team;
import org.springframework.stereotype.Service;

@Service
public class JoinTeamService {
    JoinTeamRepository joinTeamRepository;
    TeamRepository teamRepository;
    ContestRepository contestRepository;

    public JoinTeamService(JoinTeamRepository joinTeamRepository, TeamRepository teamRepository,
                           ContestRepository contestRepository) {
        this.joinTeamRepository = joinTeamRepository;
        this.teamRepository = teamRepository;
        this.contestRepository = contestRepository;
    }


    public boolean exit(String teamId, int stuAccount) {
        // 队长没有退出团队的操作，只能解散
        Team desTeam = this.teamRepository.findByTeamId(teamId);
        if(desTeam != null && desTeam.getCaptainAccount() != stuAccount){
            this.joinTeamRepository.deleteByTeamIdAndStuAccount(teamId, stuAccount);
            this.teamRepository.decrementTeamMemberNum(teamId);
            return this.joinTeamRepository.findByTeamIdAndStuAccount(teamId, stuAccount) == null;
        }
        return false;
    }

    public boolean checkJoined(String contestId, int stuAccount){
        return this.joinTeamRepository.findByContestIdAndStuAccount(contestId, stuAccount) != null;
    }
}
